﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Model;
using University.Workshop.Business;

namespace University.Workshop.WinApp
{
	public partial class ModifyPatient: Form
	{
        string idPatient;
        
		public ModifyPatient(Patient patient)
		{
			InitializeComponent();
            LoadPatientModify(patient);
            IssuePermits();
		}

        private void IssuePermits()
        {
            //buttonSave.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        }

        private void LoadPatientModify(Patient patient)
        {
            textBoxCI.Text = patient.CIPatient;
            idPatient = textBoxCI.Text;
            textBoxFirstName.Text = patient.FirstName;
            textBoxLastName.Text = patient.LastName;
            comboBoxGender.Text = patient.Gender;
            dateTimePickerBirthday.Text = Convert.ToString(patient.Birthday);
            textBoxPhone.Text = patient.Phone.ToString();
            textBoxCellPhone.Text = patient.CellPhone.ToString();
            textBoxEmail.Text = patient.Email;
            textBoxAddress.Text = patient.Address;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Patient patient = new Patient();
            patient.CIPatient = textBoxCI.Text;
            patient.FirstName = textBoxFirstName.Text;
            patient.LastName = textBoxLastName.Text;
            patient.Gender = comboBoxGender.Text;
            patient.Birthday = Convert.ToDateTime(dateTimePickerBirthday.Text);
            patient.Phone = int.Parse(textBoxPhone.Text);
            patient.CellPhone = int.Parse(textBoxCellPhone.Text);
            patient.Address = textBoxAddress.Text;
            patient.Email = textBoxEmail.Text;
            DialogResult confirm = MessageBox.Show("Are you sure to save the changes?", "Save", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if (confirm.Equals(DialogResult.OK))
            {
                if (PatientManager.ModifyPatient(idPatient, patient))
                {
                    MessageBox.Show("Changes saved successfully");
                }
                else
                {
                    MessageBox.Show("The changes where not made");
                }
            }
        }

        private void textBoxCI_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyPatient.SetError(textBoxCI, ValidateFields.CIValidation(textBoxCI.Text));
        }

        private void textBoxFirstName_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyPatient.SetError(textBoxFirstName, ValidateFields.FirstNameValidation(textBoxFirstName.Text));
        }

        private void textBoxLastName_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyPatient.SetError(textBoxLastName, ValidateFields.LastNameValidation(textBoxLastName.Text));
        }

        private void textBoxPhone_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyPatient.SetError(textBoxPhone, ValidateFields.PhoneValidation(textBoxPhone.Text));
        }

        private void textBoxCellPhone_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyPatient.SetError(textBoxCellPhone, ValidateFields.CellPhoneValidation(textBoxCellPhone.Text));
        }

        private void textBoxEmail_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyPatient.SetError(textBoxEmail, ValidateFields.EmailValidation(textBoxEmail.Text));
        }

        private void textBoxAddress_Validating(object sender, CancelEventArgs e)
        {
            errorProviderModifyPatient.SetError(textBoxAddress, ValidateFields.AddressValidation(textBoxAddress.Text));
        }
	}
}
